package cola.rh.System.Service.impl;

import cola.rh.System.Entity.User;
import cola.rh.System.Entity.vo.RouterVo;
import cola.rh.System.Entity.vo.UserQueryVo;
import cola.rh.System.Mapper.UserMapper;
import cola.rh.System.Service.MenuService;
import cola.rh.System.Service.UserService;
import cola.rh.System.common.result.PublicCodeEnum;
import cola.rh.System.common.utils.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Name: UserServiceImpl
 * @Author: Cola
 * @Time: 2023/3/7 14:44
 * @Description: UserServiceImpl
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private MenuService menuService;

    @Override
    public IPage<User> pageQueryUser(Long page, Long limit, UserQueryVo UserQueryVo) {
        /*
        创建 PageInfo 对象
        */
        Page<User> pageInfo = new Page<>(page, limit);

        /*
        封装查询条件
        */
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        String keyword = UserQueryVo.getKeyword();
        String createTimeBegin = UserQueryVo.getCreateTimeBegin();
        String createTimeEnd = UserQueryVo.getCreateTimeEnd();
        if (!StringUtils.isEmpty(keyword)) {
            /*条件存在，进行封装*/
            wrapper.
                    like(User::getUsername, keyword).
                    or().like(User::getName, keyword).
                    or().like(User::getPhone, keyword);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            /*大于*/
            wrapper.ge(User::getCreateTime, createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            /*小于*/
            wrapper.le(User::getCreateTime, createTimeEnd);
        }


        IPage<User> pageModel = this.page(pageInfo, wrapper);
        return pageModel;
    }

    @Override
    public void saveByMd5(User user) {
        /*使用 Md5 加密密码*/
        String passwordMd5 = MD5Util.encrypt(user.getPassword());
        user.setPassword(passwordMd5);
        this.save(user);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        User user = this.getById(id);
        if (status.intValue() == PublicCodeEnum.STATUS_OK.getCode()) {
            user.setStatus(status);
        } else {
            user.setStatus(0);
        }
        this.updateById(user);
    }

    /**
     * 根据 用户名获取 用户
     *
     * @param username username
     * @return 用户
     */
    @Override
    public User getByUsername(String username) {
        User user = this.getOne(new LambdaQueryWrapper<User>().
                eq(User::getUsername, username));
        return user;
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {
        Map<String, Object> result = new HashMap<>();
        User user = this.getByUsername(username);

        /*根据用户 ID 获取菜单列表并动态构建路由*/
        List<RouterVo> routerVoList = menuService.findUserMenuList(user.getId());
        /*根据用户 ID 获取按钮权限*/
        List<String> permsList = menuService.findUserPermsList(user.getId());

        result.put("name", user.getName());
        result.put("avatar", user.getHeadUrl());
        /*当前权限控制使用不到，我们暂时忽略*/
        /*result.put("roles", new HashSet<>());*/
        result.put("roles", "[Admin]");
        result.put("buttons", permsList);
        result.put("routers", routerVoList);
        return result;
    }

}
